<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleCreate"
        >
          新增
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record.id),
            },
            {
                color: 'error',
                label: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDelete.bind(null, record.id),
                },
              },
          ]"
        />
      </template>
    </BasicTable>
    <AddNameModal @register="registerModal" @success="handleSuccess" />
    <EditDetailModal @register="registerEditDetailModal" @success="handleSuccess" />    </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { defHttp } from '@/utils/http/axios';
export default defineComponent({
  name: 'ChargeStandard',
});
</script>
<script lang="ts" setup>
import { ref } from 'vue';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { useModal } from '@/components/Modal';
import AddNameModal from './AddNameModal.vue';
import EditDetailModal from './EditDetailModal.vue';
import { useMessage } from '@/hooks/web/useMessage';
const delLoading = ref(false);
const { createMessage } = useMessage();
const [registerModal, { openModal }] = useModal();
const [registerEditDetailModal, { openModal: openEditDetailModal }] = useModal();

const [registerTable, { reload }] =
  useTable({
    api: '/chargeStandard/ChargeStandard/list',
    columns: [
      {
          title: 'ID',
          dataIndex: 'id',
          width: 50,
      },
      {
          title: '标准名称',
          dataIndex: 'name',
          width: 300,
      }, 
    ],
    useSearchForm: true,
    formConfig: {
      schemas: [
        {
          component: 'Input',
          label: '标准名称',
          field: 'name',
        },
      ]
    },
    showIndexColumn: false,
    indexColumnProps: {
      fixed: 'left',
    },
    pagination: true,
    bordered: true,
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    rowKey: 'id',
  });

  function handleCreate() {
  openModal(true, {
      isUpdate: false
  });
}
function handleEdit(id: number) {
  openEditDetailModal(true, {
      id,
  });
}
async function handleDelete(id: number) {
  delLoading.value = true;
  try {
      const res = await defHttp.post({ url: `/chargeStandard/ChargeStandard/delete/${id}`});
      createMessage.success(res);
      reload();
  } finally {
      delLoading.value = false;
  }
}
function handleSuccess() {
  reload();
}
</script>
